<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜品管理</el-breadcrumb-item>
      <el-breadcrumb-item>修改菜品信息</el-breadcrumb-item>
    </el-breadcrumb>
  <el-card>
    <el-form :model="caipin" :rules="rules" ref="caipin" label-width="120px">
    <el-form-item label="菜品名称：" prop="cp_name">
      <el-input v-model="caipin.cp_name"></el-input>
    </el-form-item>
    <el-form-item label="菜品类别：" prop="lb_id">
      <el-radio-group v-model="caipin.cp_lb">
        <el-radio label="主菜"></el-radio>
        <el-radio label="凉菜"></el-radio>
        <el-radio label="酒"></el-radio>
        <el-radio label="小吃"></el-radio>
        <el-radio label="炒饭"></el-radio>
        <el-radio label="饮料"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="菜品单价：" prop="price">
      <el-input v-model="caipin.price"></el-input>
    </el-form-item>
    <el-form-item label="菜品介绍：" prop="mssage">
      <el-input type="textarea" v-model="caipin.mssage"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('caipin')">保存</el-button>
      <el-button @click="resetForm('caipin')">重置</el-button>
    </el-form-item>
  </el-form>
  </el-card>
</div>
</template>

<script>
export default {
  data () {
    return {
      caipin: {
        cp_name: '',
        cp_lb: '',
        price: '',
        mssage: ''
      },
      rules: {
        cp_name: [
          { required: true, message: '请输入菜品名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        cp_lb: [
          { required: true, message: '请选择菜品类别', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请填写菜品价格', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        console.log(this.caipin)
        if (!valid) return
        this.$http.put(`cp${this.$route.params.id}`, this.caipin).then(res => {
          this.$message({
            message: '菜品信息修改成功',
            type: 'success'
          })
          this.$router.push('/cp/listcp')
        })
      }
      )
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    fetch () {
      // 获取前端路由vue-router中的id
      this.$http.get(`cp${this.$route.params.id}`).then(res => {
        this.caipin = res.data
      })
    }
  },
  created () {
    this.fetch()
  }
}
</script>
